package com.cmd.driver;


import com.alibaba.fastjson.JSONObject;
import com.mgr.DriverOrderMgr;
import com.mgr.UserOrderMgr;
import com.player.DriverUser;
import com.player.User;
import com.qy.bean.OrderDriver;
import com.qy.bean.OrderInfo;
import com.qy.util.StaticData;
import com.util.AbstractCommand;
import com.util.PBMessage;

/**
 * 
 * 接受订单
 * @author zwl
 * 2018年2月1日
 */
public class AcceptOrderCMD  extends AbstractCommand{

	@Override
	public void execute(User user, PBMessage packet) throws Exception {
		/*
		 * 1.判断司机之前的订单是否完成
		 * 2.接受订单
		 * 修改原来的订单状态为以接单
		 * 生成一条服务状态的服务订单
		 */
		JSONObject reJs = new JSONObject();
		reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
		JSONObject param = packet.getMessage();
		if(!param.containsKey("orderId")){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			user.sendPacket(reJs);
			return;
		}
		DriverUser driverUser = (DriverUser) user;
		if(driverUser.getServerOrder()!=null){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.NOT_END_SEVER.getType());
			user.sendPacket(reJs);
			return; 
		}
		//订单id
		int orderId = param.getIntValue("orderId");
		
		synchronized (this){
		    OrderInfo orderInfo = UserOrderMgr.getOrderInfo(orderId);
		    if(orderInfo==null||
			    orderInfo.getOrderStatus()!=StaticData.OrderStatus.PAY_NOT_ACCEPT.getType()
			    ||orderInfo.getOrderStatus()!=StaticData.OrderStatus.COMPLET_CHECK_NOT_ACCEPT.getType()){
			    reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ALREADY_ACCEPT.getType());
			    user.sendPacket(reJs);
			    return; 
			}
			/*
			 * 生成一条服务状态的服务订单
			 */
			OrderDriver orderDriver = DriverOrderMgr.createOd(user.getID(), orderInfo);
			//设置司机正在服务的订单
			driverUser.setServerOrder(orderDriver);
		}
		

		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		user.sendPacket(reJs);
		return;
	}

}
